package com.lktx.sso.controller;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.data.model.request.ClientIdAndSecretModel;
import cn.dev33.satoken.oauth2.processor.SaOAuth2ServerProcessor;
import cn.dev33.satoken.oauth2.template.SaOAuth2Util;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.plugin.web.annotation.Controller;
import cn.hserver.plugin.web.annotation.RequestMapping;
import cn.hserver.plugin.web.context.HServerContextHolder;
import cn.hserver.plugin.web.context.Response;
import cn.hserver.plugin.web.interfaces.HttpRequest;
import cn.hserver.plugin.web.interfaces.HttpResponse;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lktx.sso.admin.mapper.SsoUserMapper;
import com.lktx.sso.config.OAuthConfig;
import com.lktx.sso.config.SSoSaOAuth2ServerProcessor;

import java.nio.charset.StandardCharsets;
import java.util.Map;


@Controller
public class OAuthController {

    // 模式一：Code授权码 || 模式二：隐藏式
    @RequestMapping("/{appId}/oauth2/authorize")
    public void authorize(String appId, HttpRequest request, HttpResponse httpResponse) {
        SaOAuth2ServerProcessor.instance.authorize();
    }

    // 用户登录
    @RequestMapping("/{appId}/oauth2/doLogin")
    public Object doLogin(String appId) {
        return SaOAuth2ServerProcessor.instance.doLogin();
    }

    // 用户确认授权
    @RequestMapping("/{appId}/oauth2/doConfirm")
    public Object doConfirm(String appId) {
        return SaOAuth2ServerProcessor.instance.doConfirm();
    }

    // Code 换 Access-Token || 模式三：密码式
    @RequestMapping("/{appId}/oauth2/token")
    public Object token(String appId) {
        Object token = SaOAuth2ServerProcessor.instance.token();
        System.out.println(JSONUtil.toJsonStr(token));
        return token;
    }

    // Refresh-Token 刷新 Access-Token
    @RequestMapping("/{appId}/oauth2/refresh")
    public Object refresh(String appId) {
        return SaOAuth2ServerProcessor.instance.refresh();
    }

    // 回收 Access-Token
    @RequestMapping("/{appId}/oauth2/revoke")
    public Object revoke(String appId) {
        try {
            String accessToken = SaOAuth2Manager.getDataResolver().readAccessToken(SaHolder.getRequest());
            Object loginId = SaOAuth2Util.getLoginIdByAccessToken(accessToken);
            SaResult revoke = (SaResult) SaOAuth2ServerProcessor.instance.revoke();
            if (revoke.getCode() == 200) {
                StpUtil.logout(loginId);
            }
            return revoke;
        }catch (Exception e) {
            return SaResult.error();
        }
    }

    // 模式四：凭证式
    @RequestMapping("/{appId}/oauth2/client_token")
    public Object clientToken(String appId) {
        return SaOAuth2ServerProcessor.instance.clientToken();
    }

}
